﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    public static class TextureMaker
    {
        public static Texture2D Gray(float gray)
        {
            return Monochromatic(new Color(gray,gray,gray,1f));
        }

        public static Texture2D Gray(int size,float gray)
        {
            return Monochromatic(size,new Color(gray, gray, gray, 1f));
        }


        /// <summary>
        /// 单色
        /// </summary>
        public static Texture2D Monochromatic(Color color)
        {
            Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Point;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            texture2D.SetPixel(0, 0, color);
            texture2D.Apply();
            return texture2D;
        }
        public static Texture2D Monochromatic(int size, Color color)
        {
            Texture2D texture2D = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Bilinear;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    texture2D.SetPixel(i, j, color);
                }
            }
            texture2D.Apply();
            return texture2D;
        }

        /// <summary>
        /// 纹理点乘效果：用于2个颜色效果作交集，一般 作原点效果
        /// </summary>
        public static Texture2D Dot(int size,Color fg,Color bg)
        {
            Texture2D texture = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture.filterMode = FilterMode.Bilinear;
            texture.wrapMode = TextureWrapMode.Clamp;
            int num = size / 2;
            int num2 = num * num;
            for(int i = 0; i < size; i++)
            {
                for(int j = 0; j < size; j++)
                {
                    int num3 = i - num;
                    int num4 = j - num;
                    if(num3 * num3 + num4 * num4 < num2)
                    {
                        texture.SetPixel(i, j, fg);
                    }
                    else
                    {
                        texture.SetPixel(i, j, bg);
                    }
                }
            }
            texture.Apply();
            return texture;

        }

        /// <summary>
        /// 获取一个顶点角纹理
        /// </summary>
        public static Texture2D CornerTopLeft(int size, Color fg, Color bg)
        {
            Texture2D texture2D = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Bilinear;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            int num = size / 2;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i < num && j > num)
                    {
                        texture2D.SetPixel(i, j, fg);
                    }
                    else
                    {
                        texture2D.SetPixel(i, j, bg);
                    }
                }
            }
            texture2D.Apply();
            return texture2D;
        }

        public static Texture2D CornerTopRight(int size, Color fg, Color bg)
        {
            Texture2D texture2D = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Bilinear;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            int num = size / 2;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i > num && j > num)
                    {
                        texture2D.SetPixel(i, j, fg);
                    }
                    else
                    {
                        texture2D.SetPixel(i, j, bg);
                    }
                }
            }
            texture2D.Apply();
            return texture2D;
        }

        public static Texture2D CornerBottomLeft(int size, Color fg, Color bg)
        {
            Texture2D texture2D = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Bilinear;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            int num = size / 2;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i < num && j < num)
                    {
                        texture2D.SetPixel(i, j, fg);
                    }
                    else
                    {
                        texture2D.SetPixel(i, j, bg);
                    }
                }
            }
            texture2D.Apply();
            return texture2D;
        }

        public static Texture2D CornerBottomRight(int size, Color fg, Color bg)
        {
            Texture2D texture2D = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Bilinear;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            int num = size / 2;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (i > num && j < num)
                    {
                        texture2D.SetPixel(i, j, fg);
                    }
                    else
                    {
                        texture2D.SetPixel(i, j, bg);
                    }
                }
            }
            texture2D.Apply();
            return texture2D;
        }

        /// <summary>
        /// 获取一个x图形纹理
        /// </summary>
        public static Texture2D Cross(int size, int thickness, Color fg, Color bg)
        {
            Texture2D texture2D = new Texture2D(size, size, TextureFormat.ARGB32, false);
            texture2D.filterMode = FilterMode.Bilinear;
            texture2D.wrapMode = TextureWrapMode.Clamp;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (Mathf.Abs(i - j) <= thickness || Mathf.Abs(i + j - size) <= thickness)
                    {
                        texture2D.SetPixel(i, j, fg);
                    }
                    else
                    {
                        texture2D.SetPixel(i, j, bg);
                    }
                }
            }
            texture2D.Apply();
            return texture2D;
        }

    }
}

